package com.linkage.educloud.ah.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linkage.educloud.ah.R;

import java.io.File;

/**
 * Created by Mr'Tang on 2018/9/5.
 */

public class ChoosePicDialog extends Dialog {

    private View.OnClickListener onClickListener_shot;
    private View.OnClickListener onClickListener_album;

    public ChoosePicDialog(Context context) {
        super(context, R.style.MyDialogStyleBottom);
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choosepic);
        RelativeLayout choose_shot = (RelativeLayout) findViewById(R.id.choose_shot);
        TextView choose_album = (TextView) findViewById(R.id.choose_album);
        choose_shot.setOnClickListener(onClickListener_shot);
        choose_album.setOnClickListener(onClickListener_album);
        setCanceledOnTouchOutside(true);
    }

    public void setShotListener(View.OnClickListener onClickListener) {
        this.onClickListener_shot = onClickListener;
    }

    public void setAlbumListener(View.OnClickListener onClickListener) {
        this.onClickListener_album = onClickListener;
    }

    //调用系统裁剪界面
    public static Intent startPhotoZoom(Uri uri, String savePath) {
        if (uri == null) {
            File temp = new File(Environment.getExternalStorageDirectory()
                    + savePath);
            uri = Uri.fromFile(temp);
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        return intent;
    }

    //调用系统相册界面
    public static Intent gotoAlbum() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                "image/*");
        return intent;
    }

    //调用系统拍摄界面
    public static Intent gotoShot(String savePath) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // 指定调用相机拍照后的照片存储的路径
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
                .fromFile(new File(Environment
                        .getExternalStorageDirectory(),
                        savePath)));
        return intent;
    }
}